package cn.kgc.crowdfundingadminspringboot2.service;

import cn.kgc.crowdfundingadminspringboot2.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadminspringboot2.mapper.AuthMapper;
import cn.kgc.crowdfundingadminspringboot2.pojo.Auth;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {
    @Resource
    AuthMapper authMapper;
    @Override
    public List<Auth> getAllAuth() {
        return authMapper.selectByExample(null);
    }
    @Override
    public List<Integer> getAssignedAuthIdList(Integer roleId) {
        return authMapper.getAssignedAuthIdList(roleId);
    }
    @Override
    public void updateRelationShipBetweenRoleAndAuth(Map<String, List<Integer>> assignDataMap) {
        // 1.获取两部分List数据
        List<Integer> roleIdList = assignDataMap.get("roleIdList");
        List<Integer> authIdList = assignDataMap.get("authIdList");

        // 2.取出roleId
        Integer roleId = roleIdList.get(0);

        // 3.删除旧数据
        authMapper.deleteRelationship(roleId);

        // 4.保存新数据
        if(CrowdFundingUtils.collectionEffective(authIdList)) {
            authMapper.insertRelationship(roleId, authIdList);
        }
    }
    @Override
    public List<Auth> selectByRoleIds(List<Integer> roleIds) {
        return authMapper.selectByRoleIds(roleIds);
    }
}

